﻿@model IList<UserGroupsEntity>
@{ 
	var EnableMoney = ConfigHelper.Get<UserConfig>().EnableMoney.ToString().ToLower();
	var EnableExp = ConfigHelper.Get<UserConfig>().EnableExp.ToString().ToLower();
	var EnablePoint = ConfigHelper.Get<UserConfig>().EnablePoint.ToString().ToLower();
	var EnableValidNum = ConfigHelper.Get<UserConfig>().EnableValidNum.ToString().ToLower();
	var PointName = ConfigHelper.Get<UserConfig>().PointName;
	var EnableRegCompany = ConfigHelper.Get<UserConfig>().EnableRegCompany;
}
<style>
	.layui-table-cell {
		height: auto;
		line-height:normal;
	}
</style>
<div class="layui-card">
	<div class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding-top:0px;padding-bottom:0px;" lay-filter="SearchForm">
		<div class="layui-form-item">
			<div class="layui-inline" style="margin-right: 0;">
				<label class="layui-form-label" style="padding:0px;padding-right: 10px;width:150px;">
					<select id="dropGroups">
						<option value="0">所属会员组</option>
						@foreach (var item in Model)
						{
							<option value="@item.GroupID">@item.GroupName</option>
						}
					</select>
				</label>
				<div class="layui-input-inline">
					<select id="dropSearchName">
						<option value="UserID">会员ID</option>
						<option value="UserName" selected="selected">会员名</option>
						<option value="TrueName">姓名</option>
						<option value="Email">Email</option>
						<option value="Mobile">手机号</option>
					</select>
				</div>
				<div class="layui-input-inline" style="margin: 0px;">
					<input type="text" class="layui-input" id="txtSearchKeyword" name="txtSearchKeyword" placeholder="请输入搜索关键字" autocomplete="off" onkeypress="javascript:return DefaultButton(event, 'btnSearch')">
				</div>
			</div>
			<div class="layui-inline">
				<button id="btnSearch" type="button" class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnSearch">
					<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
				</button>
			</div>
		</div>
	</div>

	<div class="layui-card-body">
		<div class="layui-tab layui-tab-brief" lay-filter="tabList">
			<ul class="layui-tab-title">
				<li lay-id="0" class="layui-this">全部</li>
				<li lay-id="1">24小时登录的</li>
				<li lay-id="2">24小时注册的</li>
				<li lay-id="3">锁定的</li>
				<li lay-id="4">未通过邮件验证的</li>
				<li lay-id="5">未通过管理员验证的</li>
				<li lay-id="6">设置了单独权限的</li>
			</ul>
			<div class="layui-tab-content">
				<table id="tblList" lay-filter="tblList"></table>
			</div>
		</div>
		<input type="hidden" id="hidTab" value="0" />
		<script type="text/html" id="toolbarHead">
			<div class="layui-form-item" style="margin: auto;">
				<div class="layui-inline">
					<button class="layui-btn layuiadmin-btn-list" onclick="layer_show('添加会员','/Admin/User/UserEdit')">添加</button>
					<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnDeleteMulti">批量删除</button>
					<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnBatchLock">批量锁定</button>
					<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnBatchPass">置为正常</button>
					<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnBatchMove">批量移动</button>
					@if (EnableMoney.ToBoolean())
					{
						<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnUserMoneyAdd">增加资金</button>
						<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnUserMoneyMinus">减少资金</button>
					}
					@if (EnableExp.ToBoolean())
					{
						<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnUserExpAdd">增加积分</button>
						<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnUserExpMinus">减少积分</button>
					}
					@if (EnablePoint.ToBoolean())
					{
						<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnUserPointAdd">增加@{@PointName}</button>
						<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnUserPointMinus">减少@{@PointName}</button>
					}
					@if (EnableValidNum.ToBoolean())
					{
						<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnUserValidNumAdd">增加有效期</button>
						<button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="btnUserValidNumMinus">减少有效期</button>
					}
				</div>
			</div>
		</script>
		<script type="text/html" id="statusText">
			<p>{{d.TrueName}}</p>
			<p>{{d.Mobile}}</p>
			<p>{{d.Email}}</p>
		</script>
		<script type="text/html" id="tblBarCol">
			<a class="layui-btn layui-btn-xs" lay-event="perm">权限</a>
			<a class="layui-btn layui-btn-xs" lay-event="move">移动</a>
			<a class="layui-btn layui-btn-xs" lay-event="lock">锁定</a>
			<a class="layui-btn layui-btn-xs" lay-event="pass">置为正常</a>
			<a class="layui-btn layui-btn-xs" lay-event="view"><i class="layui-icon layui-icon-search"></i>查看</a>
			<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
			<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
		</script>
	</div>
</div>
@Html.AntiForgeryToken()
@section scripts {
	<script type="text/javascript">
		layui.use(['form', 'table', 'element'], function () {
			var table = layui.table;
			var form = layui.form;
			var element = layui.element;

			SetSearchParam();

			//获取hash来切换选项卡，假设当前地址的hash为lay-id对应的值
			var layid = location.hash.replace(/^#tabList=/, '');
			if (layid != null && layid != "") {
				element.tabChange('tabList', layid);
				$("#hidTab").val(layid);
			}
			//监听Tab切换，以改变地址hash值
			element.on('tab(tabList)', function () {
				location.href = location.pathname + "#tabList=" + this.getAttribute('lay-id');
				window.location.reload();
			});

			var myCols = [
				{ type: "checkbox", width: 30, align: 'center' },
				{ field: 'UserName', minWidth: 100, maxWidth: 200, title: '会员名', align: 'center' },
				{ field: 'TrueName', width: 150, title: '个人信息', align: 'center', templet: '#statusText' },
				{ field: 'UserType', width: 120, title: '会员类型', align: 'center', templet: function (d) { return GetUserTypeText(d.UserType); } },
				{ field: 'GroupName', width: 120, title: '会员组', align: 'center' },
				{ field: 'LoginTime', width: 170, title: '最后登录时间/IP', align: 'center', templet: function (d) { var result = (d.LoginTime == null ? "" : (formatDateTime(d.LoginTime) == "9999-12-31 23:59:59") ? "" : formatDateTime(d.LoginTime)); return result + "<p>" + d.LoginIP + "</p>"; } },
				{ field: 'LoginTimes', width: 100, title: '登录次数', align: 'center' },
				{ field: 'UserStatus', width: 100, title: '状态', align: 'center', templet: function (d) { return GetUserStatusText(d.UserStatus); } },
				//{ field: 'UserID', width: 250, title: '设置', align: "center", templet: '#tblSetCol' },
				{ field: 'UserID', width: 470, title: '操作', align: "center", templet: '#tblBarCol' }
			];
			var EnableMoney = @EnableMoney;
			var EnableExp = @EnableExp;
			var EnablePoint = @EnablePoint;
			var EnableValidNum = @EnableValidNum;
			if (EnableValidNum) {
				myCols.splice(3, 0, { field: 'EndTime', width: 100, title: '有效期', align: 'center', templet: function (d) { return GetValidNumHtml(d.EndTime); } });
			}
			if (EnablePoint) {
				myCols.splice(3, 0, { field: 'UserPoint', width: 100, title: '@PointName', align: 'center' });
			}
			if (EnableExp) {
				myCols.splice(3, 0, { field: 'UserExp', width: 100, title: '积分', align: 'center' });
			}
			if (EnableMoney) {
				myCols.splice(3, 0, { field: 'Balance', width: 100, title: '资金', align: 'center' });
			}

			//加载表格
			table.render({
				elem: '#tblList'
				, url: '/Admin/User/GetUserList'
				, toolbar: '#toolbarHead'
				, where: GetSearchParam()
				, request: {
					pageName: 'PageNum' //页码的参数名称，默认：page
					, limitName: 'PageSize' //每页数据量的参数名，默认：limit
				}
				, cols: [myCols]
				, parseData: function (res) { //res 即为原始返回的数据
					return {
						"code": 0, //解析接口状态
						"msg": "", //解析提示文本
						"count": res.RecordTotal, //解析数据长度
						"data": res.RowList //解析数据列表
					};
				}
				, limits: [10, 15, 20, 25, 50, 100]
				, limit: 15
				, page: true
			});
			//监听表格操作列
			table.on('tool(tblList)', function (obj) {
				if (obj.event === 'view') {
					var title = "查看会员";
					var url = '/Admin/User/ViewUser?id=' + obj.data.UserID;
					var w = '820';
					var w1 = $(window).width();
					if (w > w1) {
						w = w1;
					}
					w = w + 'px';
					var h = ($(window).height() - 50) + 'px';
					var myBtn = ['关闭','发送站内信', '发送邮件', '进入会员中心'];
					@if (EnableRegCompany)
					{
						Write(Html.Raw("if (obj.data.UserType == 0) { myBtn.push('升级为商户'); }"));
					}
					layer.open({
						type: 2,
						area: [w, h],
						fix: false, //不固定
						maxmin: true,
						shade: 0.4,
						title: title,
						content: url
						, btn: myBtn
						//, yes: function (index, layero) {
							
						//}
						, btn2: function (index, layero) {
							//发送站内信
							layer_show('发送站内信', '/Admin/Plus/MessageSend?UserName=' + encodeURIComponent(obj.data.UserName));
							return false;
						}
						, btn3: function (index, layero) {
							//发送邮件
							layer_show('发送邮件', '/Admin/Plus/MailListSend?UserName=' + encodeURIComponent(obj.data.UserName));
							return false;
						}
						, btn4: function (index, layero) {
							//进入会员中心
							var url = '/Admin/User/EnterUserCenter';
							$.ajax({
								type: "POST",
								url: url,
								data: { "UserName": obj.data.UserName },
								async: false,
								headers: {
									"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
								},
								error: function (data, status, e) {
									layer.alert('加载失败!');
								},
								success: function (returnData) {
									if (returnData.Status == "1") {
										var a = document.createElement('a');
										a.target = "_blank";
										a.href = "/User/";
										a.click();
									}
									else {
										layer.msg(returnData.Msg);
									}
								}
							});
							return false;
						}
						, btn5: function (index, layero) {
							//升级为商户
							layer_show('升级为商户', '/Admin/User/RegCompany?UserID=' + obj.data.UserID);
							return false;
						}
						, success: function (layero, index) {
							//渲染按钮的样式
							addIconforBtn();
						}
					});
					return false;
				} else if (obj.event === 'edit') {
					layer_show('编辑会员', '/Admin/User/UserEdit?id=' + obj.data.UserID);
					return false;
				} else if (obj.event === 'del') {
					DeleteSingle(obj.data.UserID, "/Admin/User/DelUser");
					return false;
				} else if (obj.event === 'perm') {
					layer_show('设置会员权限', '/Admin/User/UserModifyPermissions?id=' + obj.data.UserID);
					return false;
				} else if (obj.event === 'move') {
					layer_show('移动会员', '/Admin/User/UserMove?idType=0&ids=' + obj.data.UserID);
					return false;
				} else if (obj.event === 'lock') {
					var param = { "ids": obj.data.UserID, "UserStatus": 1 };
					UpdateMulti('/Admin/User/UpdateUserStatus', param);
					return false;
				} else if (obj.event === 'pass') {
					var param = { "ids": obj.data.UserID, "UserStatus": 0 };
					UpdateMulti('/Admin/User/UpdateUserStatus', param);
					return false;
				}
			});
			//监听搜索操作
			form.on('submit(btnSearch)', function (data) {
				reloadTable();
				return false;
			});
			//监听批量删除
			form.on('submit(btnDeleteMulti)', function (data) {
				var ids = GetSelectIds();
				DeleteMulti(ids, '/Admin/User/DelUserMulti');
				return false;
			});
			//监听批量锁定
			form.on('submit(btnBatchLock)', function (data) {
				var ids = GetSelectIds();
				var param = { "ids": ids, "UserStatus": 1 };
				UpdateMulti('/Admin/User/UpdateUserStatus', param);
				return false;
			});
			//监听批量置为正常
			form.on('submit(btnBatchPass)', function (data) {
				var ids = GetSelectIds();
				var param = { "ids": ids, "UserStatus": 0 };
				UpdateMulti('/Admin/User/UpdateUserStatus', param);
				return false;
			});
			//监听批量移动
			form.on('submit(btnBatchMove)', function (data) {
				var ids = GetSelectIds();
				layer_show('移动会员', '/Admin/User/UserMove?idType=0&ids=' + ids);
				return false;
			});
			//监听增加资金
			form.on('submit(btnUserMoneyAdd)', function (data) {
				var ids = GetSelectIds();
				layer_show('增加资金', '/Admin/User/EncourageStrategy?eType=0&eAction=Add&idType=1&ids=' + ids);
				return false;
			});
			//监听减少资金
			form.on('submit(btnUserMoneyMinus)', function (data) {
				var ids = GetSelectIds();
				layer_show('减少资金', '/Admin/User/EncourageStrategy?eType=0&eAction=Minus&idType=1&ids=' + ids);
				return false;
			});
			//监听增加积分
			form.on('submit(btnUserExpAdd)', function (data) {
				var ids = GetSelectIds();
				layer_show('增加积分', '/Admin/User/EncourageStrategy?eType=1&eAction=Add&idType=1&ids=' + ids);
				return false;
			});
			//监听减少积分
			form.on('submit(btnUserExpMinus)', function (data) {
				var ids = GetSelectIds();
				layer_show('减少积分', '/Admin/User/EncourageStrategy?eType=1&eAction=Minus&idType=1&ids=' + ids);
				return false;
			});
			//监听增加点券
			form.on('submit(btnUserPointAdd)', function (data) {
				var ids = GetSelectIds();
				layer_show('增加@{@PointName}', '/Admin/User/EncourageStrategy?eType=2&eAction=Add&idType=1&ids=' + ids);
				return false;
			});
			//监听减少点券
			form.on('submit(btnUserPointMinus)', function (data) {
				var ids = GetSelectIds();
				layer_show('减少@{@PointName}', '/Admin/User/EncourageStrategy?eType=2&eAction=Minus&idType=1&ids=' + ids);
				return false;
			});
			//监听增加有效期
			form.on('submit(btnUserValidNumAdd)', function (data) {
				var ids = GetSelectIds();
				layer_show('增加有效期', '/Admin/User/EncourageStrategy?eType=3&eAction=Add&idType=1&ids=' + ids);
				return false;
			});
			//监听减少有效期
			form.on('submit(btnUserValidNumMinus)', function (data) {
				var ids = GetSelectIds();
				layer_show('减少有效期', '/Admin/User/EncourageStrategy?eType=3&eAction=Minus&idType=1&ids=' + ids);
				return false;
			});

			//表格重载
			function reloadTable(tableID = 'tblList') {
				table.reload(tableID, {
					page: {
						curr: 1
					}
					, where: GetSearchParam()
				}, 'data');
			};
			function GetSearchParam() {
				var GroupID = $("#dropGroups").val();
				var SearchName = $("#dropSearchName").val();
				var SearchKeyword = $("#txtSearchKeyword").val();
				var TabStatus = $("#hidTab").val();
				var objParam = { "GroupID": GroupID, "SearchName": SearchName, "SearchKeyword": SearchKeyword, "TabStatus": TabStatus };
				return objParam;
			}
			function SetSearchParam() {
				var GroupID = getUrlParam("GroupID", 0);
				if (GroupID == null || GroupID == "") {
					GroupID = 0;
				}
				$("#dropGroups option[value='" + GroupID + "']").attr("selected", true);
				form.render('select', 'SearchForm');
			}
			function GetSelectIds() {
				var checkStatus = table.checkStatus('tblList');
				var data = checkStatus.data;
				var ids = "";
				for (j = 0, len = data.length; j < len; j++) {
					if (j < len - 1) {
						ids += data[j].UserID + ',';
					} else {
						ids += data[j].UserID;
					}
				}
				return ids;
			}
			function addIconforBtn() {
				var btn1 = $(".layui-layer-btn .layui-layer-btn1");
				var btn2 = $(".layui-layer-btn .layui-layer-btn2");
				var btn3 = $(".layui-layer-btn .layui-layer-btn3");
				var btn4 = $(".layui-layer-btn .layui-layer-btn4");
				var btn5 = $(".layui-layer-btn .layui-layer-btn5");
				btn1.css({
					"border-color": "#1E9FFF",
					"background-color": "#1E9FFF",
					"color": "#fff"
				});
				btn2.css({
					"border-color": "#1E9FFF",
					"background-color": "#1E9FFF",
					"color": "#fff"
				});
				btn3.css({
					"border-color": "#1E9FFF",
					"background-color": "#1E9FFF",
					"color": "#fff"
				});
				btn4.css({
					"border-color": "#1E9FFF",
					"background-color": "#1E9FFF",
					"color": "#fff"
				});
				btn5.css({
					"border-color": "#1E9FFF",
					"background-color": "#1E9FFF",
					"color": "#fff"
				});
			}
		});
		function SetAdminStatus(adminID) {
			$.ajax({
				type: "POST",
				url: "/Admin/Administrator/SetAdminStatus",
				data: { "id": adminID },
				headers: {
					"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
				},
				error: function (data, status, e) {
					layer.alert('操作失败!');
				},
				success: function (data) {
					if (data.Result == "ok") {
						layer.alert('操作成功!');
						setTimeout('window.location.reload()', 1000);
					}
					else {
						layer.alert(data.Result);
					}
				}
			})
		}
	</script>
}